package tech.tongyu.bob.demo.beanlife;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component
public class User implements BeanNameAware, InitializingBean, DisposableBean {

    private String name;
    private int age;
    private String beanName;

    public User() {
        System.out.println("User instance created.");
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("Name set to: " + name);
    }

    public void setAge(int age) {
        this.age = age;
        System.out.println("Age set to: " + age);
    }

    public void displayInfo() {
        System.out.println("User info: Name=" + name + ", Age=" + age);
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("Bean Name: " + "aaa");
        this.beanName = name;
    }

    @Override
    public void afterPropertiesSet() throws Exception {

        System.out.println("InitializingBean#afterPropertiesSet called");
    }

    @PostConstruct
    public void init() {
        System.out.println("Custom init method called");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("destroy method called");
    }

    @PreDestroy
    public void preDestroy() {
        System.out.println("Custom destroy method called");
    }

}
